/**
 * 视频播放器
 * jch
 */
Ext.define('Sh.ux.VideosPlayer', {
	extend : 'Ext.panel.Panel',
	alias: 'widget.videosplayer',
	layout: 'border',
	bodyStyle: {background: 'black'},
	requires: ['Sh.ux.VideoPlayer'],
	config: {
//		src: [],
		vdata: [],
		autoPlay: false,
		autoLoadVideo: false,
		loaded: false, 
		oldtime: 0,
		onloadeddata: function(){},
		hides: [/*'play','progress','time','full','sign'*/],
		//>>>>>>>>>
		vindex: 0
	},
	initComponent : function(config) {
		var me = this;
		me.layout = 'fit';
		me.defaults = {
			listeners: {
				boxready: function(p){
					p.down('toolbar').setVisible(false);
				}
			}
		};
		me.on('boxready',function(p){
			if(me.hides && me.hides.length > 0){
				for ( var i = 0; i < me.hides.length; i++) {
					Ext.getCmp(me.id+'-'+me.hides[i]).setVisible(false);
				}
			}
			preventDefaultMenu(me.body.dom);
			me.oldtime = me.oldtime || 0;
			var time = 0,btime=0;
			for ( var i = 0; i < me.vdata.length; i++) {
				time += Number(me.vdata[i].videotime);
				if(me.oldtime < time/1000){
					me.switchVideo(i,me.oldtime-btime/1000,true);
					break;
				}
				btime += time;
			}
		});
		
		me.on('beforedestroy',function(){
			try {
				if(me.task)me.task.stop();
				$('#' + me.id + '-player').remove();
			} catch (e) {
				// TODO: handle exception
			}
		});

		me.bbar = [{
			id:me.id+'-play',
			iconCls: me.autoPlay ?'icon-v_pause':'icon-v_play',
			width:23,
			handler: function(){
				if(!me.isPlay()){ 
					me.play();
				}
				else{
					me.pause();
				}
			}
		},{
			xtype:'progressbar',
			id: me.id+'-progress',
			width:150,
			style: {cursor: 'pointer','font-size': '12px'},
			listeners:{
				afterrender: function(prbar){
					var runner = new Ext.util.TaskRunner();
					me.task = runner.newTask({
						run: function () {
							try {
								var nowtime = me.getCurrentTime();
								var alltime = me.getAllTime();
								var ts = '00:00/00:00';
								if(alltime != undefined && nowtime != undefined){
									if(!me.loaded){
										me.loaded == true;
										me.setLoaded(true);
									}
									ts = timeFormat(nowtime)+'/'+timeFormat(alltime);
									prbar.updateProgress((nowtime/alltime),'');
									if(nowtime == alltime){
										Ext.getCmp(me.id+'-play').setIconCls('icon-v_play');
										task.stop();
									}
								}
								Ext.getCmp(me.id+'-time').update({
									html: '<span style="font-size:12px;">'+ts+'</span>'
								});
								
							} catch (e) {}
						},
						interval: 500
					}).start();

					//视频进度点击事件
					prbar.el.on('click',function(e,t){
						var el = prbar.el; 
						var x = e.getX();
						var _t = (x-el.getLeft())*(me.getAllTime()/prbar.width);
						me.goToTime(_t);
					});

					prbar.addManagedListener(me,'resize', function(_me,w,h){
						if(w > 0 && h > 0){
							var prbar = Ext.getCmp(me.id+'-progress');
//							$('video',me.el.dom).width(me.body.getWidth()-2.5).height(me.body.getHeight()-5);
							me.setVideoSize(me.body.getWidth()-2.5, me.body.getHeight()-5);
							var items = prbar.ownerCt.items.items;
							var ow = 5;
							for ( var i = 0; i < items.length; i++) {
								if(prbar != items[i] && items[i].isVisible() && items[i].xtype != 'tbfill'){
									ow += items[i].getWidth()+3;
								}
							}

							prbar.setWidth(w - ow);
						}
					});
				}
			}

		},'->',{
			width:80,
			html:'<span style="font-size:12px;">00:00/00:00</span>',
			id: me.id+'-time',
			disabled:true
		},{
			width:23,
			id:me.id+'-full',
			iconCls: 'icon-v_full_0',
			handler: function(){
				me.fullVideo();
			}
		},{
			width:23,
			id:me.id+'-sign',
			iconCls: 'icon-v_md',
			handler: function(){
				Ext.getCmp(me.id+'-play').setIconCls('icon-v_play');
				me.pause();
				me.signVideo();
			}
		}];

		me.callParent();
	},
	switchVideo: function(index,time,init){
		var me = this;
		index = index || 0;
		time = time || 0;
		var vdata = me.vdata[index];
		if(vdata){
			me.vindex = index;
			me.removeAll();
			var p = {
				xtype: 'player',
				autoPlay: true,
				vdata: vdata,
				src: vdata.synpath,
				oldtime: time,
				hides: me.hides
			};
			if(init)Ext.apply(p,{autoPlay: me.autoPlay,autoLoadVideo: me.autoLoadVideo,onloadeddata: me.onloadeddata});
			me.add(p);
		}
	},
	isPlay: function(){
		var p = this.getPlayer();
		return !p ? false : !p.paused;
	},
	playOrPause: function(){
		var me = this;
		try {
			var isplay = this.isPlay();
			if(isplay){
				me.play();
			}
			else{
				me.pause();
			}
		} catch (e) {
			// TODO: handle exception
		}
	},
	getPlayer: function(){
		var me = this;
		return $("video",me.el.dom).get(0);
	},
	play: function(){
		try {
			var me = this;
			var p = this.getPlayer();
			$(p).trigger('play');
			if(p)Ext.getCmp(me.id+'-play').setIconCls('icon-v_pause');
		} catch (e) {
		}
		
	},
	pause: function(){
		try {
			var me = this;
			var p = this.getPlayer();
			$(p).trigger('pause');
			if(p)Ext.getCmp(me.id+'-play').setIconCls('icon-v_play');
		} catch (e) {
		}
	},
	getAllTime: function(){
		var me = this;
		var vs = me.vdata;
		var time = 0;
		for ( var i = 0; i < me.vdata.length; i++) {
			time += Number(me.vdata[i].videotime);
		}
		return time/1000;
	},
	goToTime: function(t){
		var me = this;
		var vs = me.vdata;
		var time = 0,btime=0;
		for ( var i = 0; i < me.vdata.length; i++) {
			time += Number(me.vdata[i].videotime);
			if(t < time/1000){
				if(me.vindex != i){
					me.switchVideo(i,t-btime/1000);
				}
				else{
					me.getPlayer().currentTime = t-btime/1000;
				}
				
				break;
			}
			btime += time;
		}
	},
	getCurrentTime: function(){
		var me = this;
		var time = 0;
		for ( var i = 0; i < me.vindex; i++) {
			time += Number(me.vdata[i].videotime);
		}
		
		return time/1000 + me.getPlayer().currentTime;
	},
	videoClear: function(){
		$('video',this.el.dom).remove();
	},
	setVideoSize: function(width,height){
		$('video',this.el.dom).width(width).height(height);
	},
	signVideo: function(){
		var me = this;
		Ext.create('Module.wg.WgEditWindow',{
			p: me,
			vdata: me.vdata[me.vindex],
			player: me,
			maximized: true,
			closeAction: 'destroy'
		}).show();
	},
	fullVideo:function(isplay){
		var me = this;
		Ext.create('Ext.window.Window',{
			width: 400,
			height: 300,
			maximized: true,
			modal: true,
			layout: 'fit',
			items: [{
				xtype: 'videosplayer',
				src: me.src,
				vdata: me.vdata,
				autoPlay: me.isPlay(),
				autoLoadVideo: true,
				oldtime: me.getCurrentTime(),
				hides: me.hides,
				onloadeddata: function(){
					
				}
			}],
			listeners: {
				afterrender: function(w){
					var p = w.down('videosplayer');
					me.pause();
					Ext.getCmp(p.id+'-full').setIconCls('icon-v_full_1');
					p.fullVideo = function(){
						p.up('window').close();
					};
				},
				close: function(w){
					var p = w.down('videosplayer');
					me.goToTime(p.getCurrentTime());
					if(p.isPlay()){
						me.play();
					}
				}
			}
		}).show();
	}
});

